HEX
Server: Apache/2.4.68 (Debian)
System: Linux as-cs-widget-demo-us-central1 6.1.0-44-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.164-1 (2026-03-09) x86_64
User: root (0)
PHP: 8.2.32
Disabled: NONE
Upload Files
File: /var/www/kevin-demo/wp-content/plugins/grocerslist/includes/Frontend/WprmPrintIntegration.php
<?php

namespace GrocersList\Frontend;

use GrocersList\Service\CreatorSettingsFetcher;
use GrocersList\Support\Config;

class WprmPrintIntegration
{
    private CreatorSettingsFetcher $creatorSettingsFetcher;
    private ClientScripts $clientScripts;

    public function __construct(CreatorSettingsFetcher $creatorSettingsFetcher, ClientScripts $clientScripts)
    {
        $this->creatorSettingsFetcher = $creatorSettingsFetcher;
        $this->clientScripts = $clientScripts;
    }

    public function register(): void
    {
        add_action('wprm_print_head', [$this, 'emitPrintHead']);
        add_action('wprm_print_footer', [$this, 'emitPrintFooter']);
    }

    public function emitPrintHead(): void
    {
        $window_grocersList = $this->clientScripts->buildWindowGrocersList();

        echo '<script>window.grocersList = ' . wp_json_encode($window_grocersList) . ';</script>' . "\n";

        $membershipsFullyEnabled = $this->creatorSettingsFetcher->getMembershipsFullyEnabled();
        $externalJsUrl = Config::getExternalJsUrl();

        if ($membershipsFullyEnabled && !empty($externalJsUrl)) {
            $versionedUrl = add_query_arg('ver', $this->clientScripts->getCacheBustingString(), $externalJsUrl);

            echo '<link rel="preload" href="' . esc_url($versionedUrl) . '" as="script">' . "\n";
            echo '<script src="' . esc_url($versionedUrl) . '" async></script>' . "\n";
        }
    }

    public function emitPrintFooter(): void
    {
        $versionedBundleUrl = add_query_arg('ver', $this->clientScripts->getCacheBustingString(), $this->clientScripts->getBundleUrl());

        echo '<script src="' . esc_url($versionedBundleUrl) . '" defer></script>' . "\n";
    }
}